/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wargame.dao;

import wargame.entity.*;
import connectionmanager.DB;
import java.util.ArrayList;

/**
 *
 * @author Clarence
 */
public class BidDAO {

    public static Bid retrieveBid(int bid_id) {
        String sql = "SELECT `bid_id`, `player_id`, `project_id`, `amount` FROM `bid` WHERE bid_id = '" + bid_id + "'";
        DB db = new DB();
        db.connect();
        db.query(sql);
        Bid b = null;
        if (db.next()) {
            b = new Bid();
            b.setBid_id(db.getInt("bid_id"));
            b.setPlayer_id(db.getInt("player_id"));
            b.setProject_id(db.getInt("project_id"));
            b.setAmount(db.getInt("amount"));
        }
        db.close();
        return b;
    }

    public static boolean updateBid(Bid bid) {
        try {
            String sql = "UPDATE bid SET"
                    + " amount = " + bid.getAmount()
                    + " WHERE project_id=" + bid.getProject_id() + " AND player_id=" + bid.getPlayer_id();
            DB db = new DB();
            db.connect();
            db.execute(sql);
            db.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static boolean deleteBid(Bid bid) {
        try {
            String sql = "DELETE FROM bid "
                    + " WHERE project_id=" + bid.getProject_id() + " AND player_id=" + bid.getPlayer_id();
            DB db = new DB();
            db.connect();
            db.execute(sql);
            db.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static int insertBid(Bid b) {
        String sql = "INSERT INTO bid (`player_id`, `project_id`, `amount`) VALUES ('" + b.getPlayer_id() + "', '" + b.getProject_id() + "', '" + b.getAmount() + "');";
        DB db = new DB();
        db.connect();
        db.execute(sql);
        int lastInsertedId = db.getLastInsertedID();
        db.close();
        return lastInsertedId;
    }

    public static ArrayList<Bid> retrieveAllBidsOfProject(int project_id) {
        ArrayList<Bid> bids = new ArrayList<Bid>();
        String sql = "SELECT `bid_id` FROM `bid` WHERE `project_id` = '" + project_id + "'";
        DB db = new DB();
        db.connect();
        db.query(sql);
        while (db.next()) {
            Bid b = BidDAO.retrieveBid(db.getInt("bid_id"));
            bids.add(b);
        }
        return bids;
    }

    public static ArrayList<Bid> retrieveAllBidsOfPlayer(int player_id) {
        ArrayList<Bid> bids = new ArrayList<Bid>();
        String sql = "SELECT `bid_id` FROM `bid` WHERE `player_id` = '" + player_id + "'";
        DB db = new DB();
        db.connect();
        db.query(sql);
        while (db.next()) {
            Bid b = BidDAO.retrieveBid(db.getInt("bid_id"));
            bids.add(b);
        }
        return bids;
    }

    public static void clear(){
         String sql = "TRUNCATE TABLE bid";
        DB db = new DB();
        db.connect();
        db.execute(sql);
        db.close();
    }
}
